package com.atguigu.system.manager;

import com.atguigu.model.system.SysRoleMenu;
import com.atguigu.system.mapper.SysRoleMenuMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * SysRoleMenuManager
 * </p>
 *
 * @author flyduck
 * @since 2023/10/26
 */
@Component
public class SysRoleMenuManager extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> {
    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    /**
     * 根据角色id查询
     * @param roleId
     * @return
     */
    public List<SysRoleMenu> findByRoleId(Long roleId) {
        QueryWrapper<SysRoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", roleId);
        return sysRoleMenuMapper.selectList(queryWrapper);
    }

    /**
     * 根据角色id删除
     * @param roleId
     */
    public void deleteByRoleId(Long roleId) {
        QueryWrapper<SysRoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", roleId);
        sysRoleMenuMapper.delete(queryWrapper);
    }

    /**
     * 批量新增
     * @param roleId
     * @param menuIdList
     */
    public void insertList(Long roleId, List<Long> menuIdList) {
        List<SysRoleMenu> list = new ArrayList<>();
        for (Long menuId : menuIdList) {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setRoleId(roleId);
            sysRoleMenu.setMenuId(menuId);
            list.add(sysRoleMenu);
        }
        this.saveBatch(list);
    }
}
